const app = getApp();
var api = require("../../../api/api.js");
var event = require("../../../event/event.js");
var utils = require("../../../utils/utils.js");
var isFormOuter;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addrList: [],
    idArr: "",
    orderHave: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    if (options.idArr) {
      //是否选中商品再选地址
      this.data.idArr = options.idArr
    }
    
    isFormOuter = options.form_outer;
    event.on('addr_add', this, function (data) {
      this.setData({
        orderHave: true,
      });
      that.getAddrList();
     });
    this.getAddrList();
  },

  radioChange: function (e) {
    this.motifyAddr(e.detail.value);
  },



  feedbackSuccessCallback: function (data) {
    if (data.data.items.length <= 0) {
      this.setData({
        orderHave: false,
      });
      return
    }
    this.data.addrList=[];
    this.setData({
      addrList: data.data.items,
    });
  },

  motifyAddr: function (commodity_id) {
    api.default.api('modify_address', { "address_id": parseInt(commodity_id), "is_default": true, "commodity_id": commodity_id }, function (data) {
      console.log("motify ok");

    }, function (e, du) {

    });

  },

  deleteAddr: utils.throttle(function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success: function (res) {
        if (res.confirm) {
          api.default.api('delete_address', { "address_id": parseInt(e.currentTarget.id) }, function (data) {
            //app.log("addr_list","dele ok");
            that.data.addrList = [];
            that.getAddrList();
          }, function (e, du) {
            console.log(du + "---" + e);
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }),

  addAddr: function () {
    wx.navigateTo({
      url: '../addr_add/addr_add'
    });


  },

  seleAddr: function (e) {
    if (isFormOuter) {
      var aid = e.currentTarget.id;
      console.log("aid=" + aid);
      var list = this.data.addrList;
      for (var i = 0; i < list.length; i++) {
        if (list[i].address_id == aid) {
          event.notify("addr", list[i]);
          wx.navigateBack({});
          break;
        }

      }
    }


  },

  editAddr: function (e) {

    var commodityId = e.currentTarget.id;
    var list = this.data.addrList;
    var curAddr;
    for (var i = 0; i < list.length; i++) {
      if (commodityId == list[i].address_id) {
        curAddr = list[i];
        break;
      }
    }
    wx.navigateTo({
      url: '../addr_add/addr_add?curAddr=' + JSON.stringify(curAddr)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  

  },

  getAddrList: function () {
    var that = this;
    var payload = {};
    if (this.data.idArr) {
      payload.commodity_ids = this.data.idArr.split(",")
    }
    api.default.api('get_address_list', payload, that.feedbackSuccessCallback, that.feedbackErrorCallback);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.addrList.length <= 0) {
      event.notify("defaultNull", "");

    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


})